package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,HttpSession session){


        //1,先根据用户名查询
        //前端传过来的用户名
        String username = employee.getUsername();

        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);//用户名条件
        Employee emp = employeeService.getOne(queryWrapper);
        if(emp==null){
            return R.error("用户名不存在");
        }

        //2，在比较密码（数据库密码，提交密码）
        String pw1 = emp.getPassword();
        String pw2 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());//密码需要md5加密
        if(!pw1.equals(pw2)){
            return R.error("密码错误");
        }

        //3,判断员工状态（1代表启动，0代表禁用）
        Integer status = emp.getStatus();
        if(status!=1){
            return R.error("员工账号已被禁用");
        }

        //4,登录成功，响应 code=1，data=0，并将员工id存入session
        session.setAttribute("employee",emp.getId());
        return R.success(emp);

    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("logout")
    public  R<String> logout(HttpSession session){
        //销毁session
        session.invalidate();
        return R.success("退出登录成功");
    }


    /**
     * 添加员工
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Employee employee,HttpSession session){
        //设置默认密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置操作时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //设置操作人
        //employee.setCreateUser((Long) session.getAttribute("employee"));
        //employee.setUpdateUser((Long) session.getAttribute("employee"));
        //调用业务层天添加数据
        employeeService.save(employee);
        return R.success("添加员工成功");

    }

    /**
     * 带条件的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,@RequestParam(required = false) String name){
        //构造分页结构
        Page<Employee> pageInfo= new Page<>(page,pageSize);
        //构造查询条件
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        //StringUtils.isNotBlank(name):工具类，如果不为空
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        //添加排序条件
        queryWrapper.orderByDesc("update_time");
        Page<Employee> page1 = employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);

    }

    /**
     * 更新状态信息
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee ,HttpSession session){
        //设置更新时间
        //employee.setUpdateTime(LocalDateTime.now());
        //设置更新人员
        //employee.setUpdateUser((Long) session.getAttribute("employee"));

        employeeService.updateById(employee);
        return R.success("更新成功");

    }

    /**
     * 根据id修改
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }




}
